﻿/******************************************************************
** 文 件 名: BD_Contract_Edit.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-8 2:35:58
** 描    述: 基础资料-合同信息编辑页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;


namespace Honestysoft.Financing.WebUI.Module.Contract
{
	public partial class BD_Contract_Edit : BasePage
	{	
		protected override void Page_Load(object sender, EventArgs e)
        {
			base.Page_Load(sender, e);
            if (!IsPostBack)
            {
				BindControl();
                Bind();
            }
        }
		
		private void BindControl()
		{
			ControlHelper.BindDictionary(ddlContractCategory, "ContractCategory");
            //numTotalPrice.Attributes.Add("onpropertychange", "javascript:document.getElementById('" + hidChangePrice.ClientID + "').value=this.value;document.getElementById('" + numChangePrice.ClientID + "').value=this.value;"); 
		}

        private void Bind()
        {
            BD_Contract obj = null;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //hidID.Value = Request.QueryString["ID"];
                obj = new Honestysoft.Financing.BusinessLogic.Contract(Globals.CurrentConnStringName).GetContract(Request.QueryString["ID"]);// new BaseBusinessLogic<BD_Contract>().GetEntity(Request.QueryString["ID"]);

                hidProjectID.Value = obj.ProjectID;

                txtProject.Text = obj.ProjectName;// new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetProjectName(obj.ProjectID);
                //txtProject.Enabled = false;
                ddlContractCategory.SelectedValue = obj.ContractCategory;
                txtCode.Text = obj.Code;
                txtName.Text = obj.Name;
                dtpSignDate.Text = (obj.SignDate.HasValue ? obj.SignDate.Value.ToString("yyyy-MM-dd") : "");
                numTotalPrice.Text = obj.TotalPrice.ToString("0.00");

                numChangePrice.Text = obj.CurChangePrice.ToString("0.00");// obj.ChangePrice.Value.ToString("0.00");
                hidChangePrice.Value = numChangePrice.Text;

                //numChangePrice.Text = (obj.ChangePrice.HasValue ? obj.ChangePrice.Value.ToString("0.00") : "");
                //numStage.Text = obj.Stage.ToString("0.00");
                //chkIsZD.Checked = obj.IsZD;
                numReserveAmount.Text = (obj.ReserveAmount.HasValue ? obj.ReserveAmount.Value.ToString("0.00") : "");

                FCKeditor2.Value = obj.PaymentCondition;

                hidContractor.Value = obj.Contractor;
                txtContractor.Text = obj.ContractorName;// new Honestysoft.Financing.BusinessLogic.Reports(Globals.CurrentConnStringName).GetCompanyName(obj.Contractor);

                txtBankAccount.Text = obj.BankAccount;
                txtBankName.Text = obj.BankName;

                txtRemark.Text = obj.Remark;

                hidCreator.Value = obj.Creator;
                hidCreateTime.Value = obj.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");

                if (obj.IsInit)
                {
                    //numChangePrice.Enabled = false;
                    numTotalPrice.Enabled = false;
                    btnSelect2.Enabled = false;
                    txtCode.Enabled = false;
                    txtName.Enabled = false;

                    btnConfirm.Enabled = false;

                    hidIsInit.Value = "1";
                    hidConfirmOpinion.Value = obj.ConfirmOpinion;
                    hidConfirmTime.Value = obj.ConfirmTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    hidConfirmUser.Value = obj.ConfirmUser;
                         
                }
                else
                {
                    if (!obj.HasChangeRecord)
                    {
                        numTotalPrice.Attributes.Add("onpropertychange", "javascript:document.getElementById('" + hidChangePrice.ClientID + "').value=this.value;document.getElementById('" + numChangePrice.ClientID + "').value=this.value;"); 
                    }

                    btnViewConfirm.Enabled = false;
                }

            }
            else
            {
                btnConfirm.Enabled = false;
                btnViewConfirm.Enabled = false;

                //hidID.Value = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                txtProject.Text = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetProjectName(Request.QueryString["ProjectID"]);
                //txtProject.Enabled = false;
                hidProjectID.Value = Request.QueryString["ProjectID"];
                if (Request.QueryString["ContractCategory"] == "0")
                {
                    if (ddlContractCategory.Items != null && ddlContractCategory.Items.Count > 0)
                    {
                        ddlContractCategory.SelectedIndex = 0;
                    }
                }
                else
                {
                    ddlContractCategory.SelectedValue = Request.QueryString["ContractCategory"];
                }
                hidCreator.Value = UserLoginer.LoginUser.ID;
                hidCreateTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                txtCode.Text = new Honestysoft.Financing.BusinessLogic.Contract(Globals.CurrentConnStringName).GetNextContractCode(Request.QueryString["ProjectID"]);

                numTotalPrice.Attributes.Add("onpropertychange", "javascript:document.getElementById('" + hidChangePrice.ClientID + "').value=this.value;document.getElementById('" + numChangePrice.ClientID + "').value=this.value;"); 
            }


            //UcUploadify1.DataId = hidID.Value;
            //UcUploadify1.IsAutoUpload = true;
            //UcUploadify1.IsMulti = true;
            //UcUploadify1.FileStoreType = Baseframework.Model.EnumItem.StoreType.Disk;

            //btnOK.Attributes.Add("onclick", "javascript:" + UcUploadify1.GetUploadJavascriptFunctionName() + "();");
        }

        protected string GetCheckbox(string dataId)
        {
            return "";
        }
        protected string GetButtonScript(string dataId)
        {
            string url = "";
            url += "<a href=\"#\" onclick=\"javascript:setDefaultBank('" + dataId + "');\">设为默认</a>";
            if (hidIsInit.Value != "1")//已初始化
            {
                url += "&nbsp;&nbsp;<a href=\"#\" onclick=\"javascript:addDeleteBank('" + dataId + "');\">删除</a>";
            }
            return url;
        }

		protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Edit();
            }
            else
            {
                Add();
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {            
            try
            {
                BD_Contract obj = GetSubmitObject();
                int ret = new BaseBusinessLogic<BD_Contract>().CreateEntity(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {            
            try
            {
                BD_Contract obj = GetSubmitObject();
                int ret = new BaseBusinessLogic<BD_Contract>().UpdateEntity(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }
		/// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private BD_Contract GetSubmitObject()
        {
            BD_Contract obj = new BD_Contract();

           // obj.ID = hidID.Value;

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj.ID = Request.QueryString["ID"];
            }
            else
            {
                obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            }
            obj.ProjectID = hidProjectID.Value;
			obj.ContractCategory = ddlContractCategory.SelectedValue;
			obj.Code = txtCode.Text;
			obj.Name = txtName.Text;
			if(dtpSignDate.Text.Trim().Length > 0){obj.SignDate = Convert.ToDateTime(dtpSignDate.Text);}
			if(numTotalPrice.Text.Trim().Length > 0){obj.TotalPrice = Convert.ToDecimal(numTotalPrice.Text);}
			//if(numChangePrice.Text.Trim().Length > 0){obj.ChangePrice = Convert.ToDecimal(numChangePrice.Text);}

            //if (numChangePrice.Text.Trim().Length == 0)
            //{
            //    obj.ChangePrice = Convert.ToDecimal(numTotalPrice.Text);
            //}
            //else
            //{
            //    obj.ChangePrice = Convert.ToDecimal(numChangePrice.Text);
            //}
            obj.ChangePrice = Convert.ToDecimal(hidChangePrice.Value);

            //if(numStage.Text.Trim().Length > 0){obj.Stage = Convert.ToDecimal(numStage.Text);}
            //obj.IsZD = chkIsZD.Checked;

            obj.Stage = 0;
            obj.IsZD = false;

			if(numReserveAmount.Text.Trim().Length > 0){obj.ReserveAmount = Convert.ToDecimal(numReserveAmount.Text);}
            obj.Contractor = hidContractor.Value;// txtContractor.Text;
			obj.Remark = txtRemark.Text;
            obj.Creator = hidCreator.Value;
            obj.CreateTime = Convert.ToDateTime(hidCreateTime.Value);
            obj.PaymentCondition = FCKeditor2.Value;

            obj.BankName = txtBankName.Text;
            obj.BankAccount = txtBankAccount.Text;

            obj.IsInit = (hidIsInit.Value == "1");
            if (obj.IsInit)
            {                
                obj.ConfirmTime = Convert.ToDateTime(hidConfirmTime.Value);
                obj.ConfirmUser = hidConfirmUser.Value;
            }
            obj.ConfirmOpinion = hidConfirmOpinion.Value;
            return obj;
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            Bind();
        }
	}
}